package com.shilin.watchsystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shilin.watchsystem.entity.Admin;
import com.shilin.watchsystem.form.AddAdminForm;
import com.shilin.watchsystem.service.IAdminService;
import com.shilin.watchsystem.util.ResultUtil;
import com.shilin.watchsystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-08-21
 */
@RestController
@RequestMapping("/watchsystem/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;

    @PostMapping("/isLogin")
    public Object isLogin(HttpServletRequest request){
        if(request.getSession().getAttribute("admin")==null){
            return ResultUtil.error(444,"未登陆");
        }
        return ResultUtil.success();
    }

    @PostMapping("/insert")
    public Object insert(AddAdminForm form){
        adminService.insert(form);
        return ResultUtil.success();
    }


    @PostMapping("/login")
    public Object login(String account, String password, HttpServletRequest request){
        adminService.login(account,password,request);
        return ResultUtil.success();
    }

    @PostMapping("/delete")
    public Object delete(Long id){
        adminService.removeById(id);
        return ResultUtil.success();
    }

    @PostMapping("/page")
    public Object page(int page,int limit){
        Page<Admin> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(limit);
        IPage<Admin> vos = adminService.page(page1);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",vos.getRecords().size());
        map.put("msg","成功");
        map.put("data",vos.getRecords());

        return map;
    }

}
